package cn.gdut.chengcai.base;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.ui.ModelMap;

import cn.gdut.chengcai.entity.MemberInfo;
import cn.gdut.chengcai.entity.MemberInfoExample;
import cn.gdut.chengcai.entity.PersonalInfo;
import cn.gdut.chengcai.param.MemberSearchParam;
import cn.gdut.chengcai.service.MemberInfoService;
import cn.gdut.chengcai.vo.MemberInfoVo;
import cn.gdut.chengcai.vo.PersonalInfoVo;

public class MemberUtil {
	public static String DEFAULT_LOGO_URL = "";

	public static void fillVo(PersonalInfo entity, PersonalInfoVo vo) throws Exception {
		BeanUtils.copyProperties(vo, entity);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (null != entity.getCreateTime()) {
			vo.setCreateTime(sdf.format(entity.getCreateTime()));
		}
		if (null != entity.getUpdateTime()) {
			vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
		}
	}

	public static void fillVo(MemberInfo entity, MemberInfoVo vo) throws Exception {
		BeanUtils.copyProperties(vo, entity);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (null != entity.getLastestLoginTime()) {
			vo.setLastestLoginTime(sdf.format(entity.getLastestLoginTime()));
		}
		if (null != entity.getCreateTime()) {
			vo.setCreateTime(sdf.format(entity.getCreateTime()));
		}
		if (null != entity.getUpdateTime()) {
			vo.setUpdateTime(sdf.format(entity.getUpdateTime()));
		}
		if (null != entity.getIsActivated()) {
			if (entity.getIsActivated() == true) {
				vo.setIsActivated("已激活");
			} else {
				vo.setIsActivated("未激活");
			}
		}
		if (null != entity.getIsMailActivated()) {
			if (entity.getIsMailActivated() == true) {
				vo.setIsMailActivated("已激活");
			} else {
				vo.setIsMailActivated("未激活");
			}
		}
		if (null != entity.getIsAllowLogin()) {
			if (entity.getIsAllowLogin() == true) {
				vo.setIsAllowLogin("是");
			} else {
				vo.setIsAllowLogin("否");
			}
		}
		if (null != entity.getIsDeleted()) {
			if (entity.getIsDeleted() == true) {
				vo.setIsDeleted("是");
			} else {
				vo.setIsDeleted("否");
			}
		}

	}

	public static void fillVoList(List<MemberInfo> list, List<MemberInfoVo> voList) throws Exception {
		if ((null != list) && (list.size() > 0)) {
			for (MemberInfo entity : list) {
				MemberInfoVo vo = new MemberInfoVo();
				fillVo(entity, vo);
				voList.add(vo);
			}
		}
	}

	public static List<MemberInfoVo> filter(HttpSession session, ModelMap map, MemberInfoService service,
			MemberSearchParam newParam) throws Exception {
		MemberSearchParam param = null;
		MemberInfoExample example = new MemberInfoExample();
		if (null != newParam) {// 如果新参数不为空，则放到session中
			param = newParam;
		} else {// 从session中获取参数
			MemberSearchParam paramSession = (MemberSearchParam) session
					.getAttribute(ConstantUtil.SESSION_SEARCH_PARAM);
			param = paramSession;
		}
		if (param == null) {
			param = new MemberSearchParam();
			resetSearchParamConstant(param);
		} else {
			MemberInfoExample.Criteria criteria = example.createCriteria();
			if (null != param.getSn()) {
				criteria.andSnEqualTo(param.getSn());
			}
			if (null != param.getUsername()) {
				criteria.andUsernameLike("%" + param.getUsername() + "%");
			}
			if (null != param.getEmail()) {
				criteria.andEmailEqualTo(param.getEmail());
			}
			if (null != param.getIsActivated()) {
				criteria.andIsActivatedEqualTo(param.getIsActivated());
			}
		}

		List<MemberInfoVo> voList = new ArrayList<>();
		List<MemberInfo> listTotal = service.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}

		List<MemberInfo> list = listTotal.subList(fromIndex, toIndex);
		if ((null != list) && (list.size() > 0)) {
			fillVoList(list, voList);
		}
		if (map != null) {
			map.put("list", voList);
		}

		session.setAttribute("pager", pager);
		session.setAttribute(ConstantUtil.SESSION_SEARCH_PARAM, param);

		return voList;
	}

	public static void resetSearchParamConstant(MemberSearchParam param) {
		// 恢复默认值
		// param.setPageNo("1");
		param.setSn(null);
		param.setUsername(null);
		param.setEmail(null);
		param.setIsActivated(true);

	}

}
